<template>
  <div>
    <!--详情-->
    <div class="jumbotron">
      <h2>{{ news.title }}</h2>
      <p>{{ news.content }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      news: {
        id: "",
        title: "",
        content: ""
      }
    };
  },

  components: {},

  methods: {
    getdata(id) {
      if (id) {
        this.$axios.get("http://localhost:8080/news.json").then(res => {
          console.log(res, 899);
          this.news = res.data.filter(item => item.id == id)[0];
        });
      }
    }
  },

  watch: {
    //路由有变化才触发
    $route: {
      deep: true,
      handler(route) {
        console.log(route, 666888);
        let { id } = route.params;
        console.log(id, 7788);
        this.getdata(id);
      }
    }
  },

  //进入页面就发请求
  created() {
    console.log(this.$route, 666);
    let { id } = this.$route.params;
    this.getdata(id);
  }
};
</script>

<style scoped>
</style>